package com.middlegroup.subjectpay.service.impl;

import com.middlegroup.subjectpay.config.ConstantInfo;
import com.middlegroup.subjectpay.entity.ThirdProtocolBindCard;
import com.middlegroup.subjectpay.mapper.ThirdProtocolBindCardMapper;
import com.middlegroup.subjectpay.service.ThirdProtocolBindCardService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chengweiwei123
 * @since 2019-01-24
 */
@Service
@Slf4j
public class ThirdProtocolBindCardServiceImpl extends ServiceImpl<ThirdProtocolBindCardMapper, ThirdProtocolBindCard> implements ThirdProtocolBindCardService {

    @Resource
    private ThirdProtocolBindCardMapper thirdProtocolBindCardMapper;

    @Autowired
    private ConstantInfo constantInfo;

    @Override
    public ThirdProtocolBindCard getLastOne(ThirdProtocolBindCard thirdProtocolBindCard) {
        try {
            if (thirdProtocolBindCard != null) {
                thirdProtocolBindCard.SetEncryptAll(constantInfo.getAeskey());
            }
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        // 查询记录
        ThirdProtocolBindCard bindCard = thirdProtocolBindCardMapper.getLastOne(thirdProtocolBindCard);
        try {
            if (bindCard != null) {
                bindCard.GetDecryptAll(constantInfo.getAeskey());
            }
        } catch (Exception e) {
            log.error("四要素解密异常：{}", e);
        }
        return bindCard;
    }

    @Override
    public boolean insert(ThirdProtocolBindCard entity) {
        try {
            entity.SetEncryptAll(constantInfo.getAeskey());
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        return super.insert(entity);
    }

    @Override
    public boolean updateById(ThirdProtocolBindCard entity) {
        try {
            entity.SetEncryptAll(constantInfo.getAeskey());
        } catch (Exception e) {
            log.error("四要素加密异常：{}", e);
        }
        return super.updateById(entity);
    }
}
